<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章分类" prop="cid">
        <el-select v-model="queryParams.cid" placeholder="请选择文章分类" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option v-for="(item,index) in articleCategoryList" :value="item.id" :label="item.title" :key="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入文章作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章简介" prop="synopsis">
        <el-input
          v-model="queryParams.synopsis"
          placeholder="请输入文章简介"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章分享标题" prop="shareTitle">
        <el-input
          v-model="queryParams.shareTitle"
          placeholder="请输入文章分享标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章分享简介" prop="shareSynopsis">
        <el-input
          v-model="queryParams.shareSynopsis"
          placeholder="请输入文章分享简介"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="浏览次数" prop="visit">
        <el-input
          v-model="queryParams.visit"
          placeholder="请输入浏览次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原文链接" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入原文链接"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择文章状态" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option v-for="(item,index) in statusOptions" :value="item.dictSort" :label="item.dictLabel" :key="item.dictSort"/>
        </el-select>
      </el-form-item>
      <el-form-item label="商品关联" prop="productId">
        <el-select v-model="queryParams.productId" placeholder="请选择商品关联" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option v-for="(item,index) in goodsList" :value="item.id" :label="item.name" :key="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否热门" prop="isHot">
        <el-select v-model="queryParams.isHot" placeholder="请选择是否热门" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option v-for="(item,index) in isHotOptions" :value="item.dictSort" :label="item.dictLabel" :key="item.dictSort"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否是轮播图" prop="isBanner">
        <el-select v-model="queryParams.isBanner" placeholder="请选择是否是轮播图" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option v-for="(item,index) in isBannerOptions" :value="item.dictSort" :label="item.dictLabel" :key="item.dictSort"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:article:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:article:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="文章分类" align="center" prop="ctitle" />
      <el-table-column label="文章标题" align="center" prop="title" />
      <el-table-column label="文章作者" align="center" prop="author" />
      <el-table-column label="文章图片" align="center" prop="imageInput" >
        <template slot-scope="scope">
          <el-image v-if="scope.row.imageInput" style="width: 50px; height: 50px" :src="viewImgUrl+'?id='+scope.row.imageInput"  :preview-src-list="[viewImgUrl+'?id='+scope.row.imageInput]" />
          <span v-else>暂无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="文章简介" align="center" prop="synopsis" />
      <el-table-column label="文章分享标题" align="center" prop="shareTitle" />
      <el-table-column label="文章分享简介" align="center" prop="shareSynopsis" />
      <el-table-column label="浏览次数" align="center" prop="visit" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="原文链接" align="center" prop="url" />
      <el-table-column label="文章状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="商品关联" align="center" prop="productName" />
      <el-table-column label="是否热门" align="center" prop="isHot" :formatter="articleHotFormat"/>
      <el-table-column label="是否是轮播图" align="center" prop="isBanner" :formatter="bannerFormat"/>
      <el-table-column label="文章内容" align="center" prop="content" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:article:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:article:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资讯信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章分类" prop="cid">
          <el-select style="width: 100%;" v-model="form.cid" placeholder="请输入文章分类">
            <el-option v-for="(item,index) in articleCategoryList" :value="item.id" :label="item.title" :key="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="文章作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入文章作者" />
        </el-form-item>
        <el-form-item label="文章图片" prop="imageInput">
          <imageUpload v-model="form.imageInput"/>
        </el-form-item>
        <el-form-item label="文章简介" prop="synopsis">
          <el-input v-model="form.synopsis" placeholder="请输入文章简介" />
        </el-form-item>
        <el-form-item label="文章分享标题" prop="shareTitle">
          <el-input v-model="form.shareTitle" placeholder="请输入文章分享标题" />
        </el-form-item>
        <el-form-item label="文章分享简介" prop="shareSynopsis">
          <el-input v-model="form.shareSynopsis" placeholder="请输入文章分享简介" />
        </el-form-item>
        <el-form-item label="浏览次数" prop="visit">
          <el-input v-model="form.visit" placeholder="请输入浏览次数" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="原文链接" prop="url">
          <el-input v-model="form.url" placeholder="请输入原文链接" />
        </el-form-item>
        <el-form-item label="文章状态">
          <el-select style="width: 100%;" v-model="form.status" placeholder="请选择文章状态">
            <el-option v-for="(item,index) in statusOptions" :value="item.dictSort" :label="item.dictLabel" :key="item.dictSort"/>
          </el-select>
        </el-form-item>
        <el-form-item label="商品关联" prop="productId">
          <el-select style="width: 100%;" v-model="form.productId" placeholder="请选择商品关联">
            <el-option v-for="(item,index) in goodsList" :value="item.id" :label="item.name" :key="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否热门" prop="isHot">
          <el-select style="width: 100%;" v-model="form.isHot" placeholder="请选择是否热门">
            <el-option v-for="(item,index) in isHotOptions" :value="item.dictSort" :label="item.dictLabel" :key="item.dictSort"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否是轮播图" prop="isBanner">
          <el-select style="width: 100%;" v-model="form.isBanner" placeholder="请选择是否是轮播图">
            <el-option v-for="(item,index) in isBannerOptions" :value="item.dictSort" :label="item.dictLabel" :key="item.dictSort"/>
          </el-select>
        </el-form-item>
        <el-form-item label="文章内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle, exportArticle,getArticleCategoryList,getGoodsList } from "@/api/basic/article";
import Editor from '@/components/Editor';
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Article",
  components: {
    Editor,
    ImageUpload
  },
  data() {
    return {
      // 后台服务器地址
      viewImgUrl: process.env.VUE_APP_BASE_API + "/common/viewImg",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 资讯信息表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cid: null,
        title: null,
        author: null,
        imageInput: null,
        synopsis: null,
        shareTitle: null,
        shareSynopsis: null,
        visit: null,
        sort: null,
        url: null,
        status: null,
        productId: null,
        isHot: null,
        isBanner: null,
        content: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "文章状态 0正常 1禁用不能为空", trigger: "blur" }
        ],
      },
      // 文章状态
      statusOptions:[],
      // 是否热门
      isHotOptions:[],
      // 是否轮播图
      isBannerOptions:[],
      //资讯分类信息
      articleCategoryList:[],
      //农资产品信息
      goodsList:[]
    };
  },
  created() {
    this.getList();
    this.getArticleCategorySelectList();
    this.getGoodsSelectList();
    this.getDicts("basic_article_status").then(response => {
      this.statusOptions = response.data;
    });
     this.getDicts("basic_articleHot_status").then(response => {
      this.isHotOptions = response.data;
    });
     this.getDicts("basic_banner_status").then(response => {
      this.isBannerOptions = response.data;
    });
  },
  methods: {
    // 查询内部字典
    statusFormat(row, column){
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    articleHotFormat(row, column){
      return this.selectDictLabel(this.isHotOptions, row.isHot);
    },
    bannerFormat(row, column){
      return this.selectDictLabel(this.isBannerOptions, row.isBanner);
    },
    /** 查询资讯信息列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取资讯分类信息下拉框
    getArticleCategorySelectList(){
      getArticleCategoryList().then(response => {
        this.articleCategoryList = response.data;
      });
    },
    // 获取农资产品信息下拉框
    getGoodsSelectList(){
      getGoodsList().then(response => {
        this.goodsList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cid: null,
        title: null,
        author: null,
        imageInput: null,
        synopsis: null,
        shareTitle: null,
        shareSynopsis: null,
        visit: null,
        sort: null,
        url: null,
        status: 0,
        createTime: null,
        productId: null,
        isHot: null,
        isBanner: null,
        content: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资讯信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArticle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资讯信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除资讯信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delArticle(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有资讯信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportArticle(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
